package net.csdn.business.webhook.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.common.annotation.Inner;
import net.csdn.business.common.domain.query.WebhookConfigQuery;
import net.csdn.business.common.domain.request.webhook.DeleteWebhookConfigDTO;
import net.csdn.business.common.domain.request.webhook.NewWebhookConfigDTO;
import net.csdn.business.common.domain.request.webhook.UpdateWebhookConfigDTO;
import net.csdn.business.common.domain.vo.WebhookConfigVO;
import net.csdn.business.webhook.service.IWebhookConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName WebhookConfigController
 * @Description webhook配置信息Controller
 * @Author zhangyl
 * @Date 2023/4/23 16:14
 */
@Inner
@Slf4j
@RestController
@RequestMapping("/api/v1/webhook")
public class WebhookConfigController
{

    @Autowired
    private IWebhookConfigService webhookConfigService;


    /**
     * 分页查询webhook配置信息列表
     */
    @PostMapping("/pagelist")
    public Page<WebhookConfigVO> findPageList(@RequestBody @Validated WebhookConfigQuery query) throws Exception
    {
        return webhookConfigService.findPageList(query);
    }

    /**
     * 根据fromId查询webhook配置信息列表
     */
    @GetMapping("/list/{fromId}")
    public List<WebhookConfigVO> findListByFromId(@PathVariable("fromId")String fromId){
        return webhookConfigService.selectWebhookConfigList(fromId);
    }


    /**
     * 根据id获取webhook配置信息详细信息
     */
    @GetMapping(value = "/{id}")
    public WebhookConfigVO getInfo(@PathVariable("id") String id) throws Exception
    {
        return webhookConfigService.selectWebhookConfigById(id);
    }

    /**
     * 新增webhook配置信息
     */
    @PostMapping("/add")
    public String add(@RequestBody @Validated NewWebhookConfigDTO params) throws Exception
    {
        log.info("webhook config add params is:{}", JSON.toJSONString(params));
        String webHookId=webhookConfigService.insertWebhookConfig(params);
        //异步发送ping事件
        webhookConfigService.sendPing(webHookId,params);
        return webHookId;
    }

    /**
     * 修改webhook配置信息
     */
    @PostMapping("/update")
    public Integer edit(@RequestBody @Validated UpdateWebhookConfigDTO params) throws Exception
    {
        log.info("webhook config update params is:{}", JSON.toJSONString(params));
        return webhookConfigService.updateWebhookConfig(params);
    }

    /**
     * 删除webhook配置信息
     */
	@PostMapping("/delete")
    public Integer delete(@RequestBody @Validated DeleteWebhookConfigDTO params)
    {
        log.info("webhook config delete params is:{}", JSON.toJSONString(params));
        return webhookConfigService.deleteWebhookConfigById(params);
    }
}
